import { defineStore } from 'pinia'

export const useTabsStore = defineStore('tabs', {
  state: () => ({
    tabs: [
      {
        name: 'dashboard',
        title: '仪表盘',
        path: '/dashboard'
      }
    ],
    activeTab: 'dashboard'
  }),
  
  actions: {
    // 添加标签页
    addTab(tab) {
      const existingTab = this.tabs.find(t => t.name === tab.name)
      if (!existingTab) {
        this.tabs.push(tab)
      }
    },
    
    // 移除标签页
    removeTab(targetName) {
      const tabs = this.tabs
      let activeTab = this.activeTab
      
      if (activeTab === targetName) {
        tabs.forEach((tab, index) => {
          if (tab.name === targetName) {
            const nextTab = tabs[index + 1] || tabs[index - 1]
            if (nextTab) {
              activeTab = nextTab.name
            }
          }
        })
      }
      
      this.activeTab = activeTab
      this.tabs = tabs.filter(tab => tab.name !== targetName)
    },
    
    // 设置当前激活的标签页
    setActiveTab(name) {
      this.activeTab = name
    }
  },
  
  persist: true
})